<?php
namespace app\app\controller\v1;
use app\app\Appbase;
use think\facade\Session;
use think\facade\Cache;
use base\pay\WxApi;
use base\service\MsgApi;

//
//会员
//
class UserVip extends Appbase 
{
	
	/**
	* 验证
	* /app/v1/user_vip/verifi
	* 是否需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function verifi(){
		try {
			return $this->ReSucess( model("userRechargeRule")->getValidList() );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 会员绑定
	* /app/v1/user_vip/binding
	* 是否需要登录 
	* @is_need_login true
	*/
	public function binding($account,$password,$phone='',$code='',$name='',$sex='',$birthday=''){
		if( !$account ||  !$password){
			return $this->ReError('参数错误！');	
		}
		$uid = request() ->userId;
		try {
			if(request()->userInfo['is_vip'] == 1){
				return $this->ReError('不能绑定多张会员卡！');	
			}
			if( !$card = model("UserMemberCard")->where('is_del',0)->where('code',$account)->find()){
				return $this->ReError('卡号不存在！');	
			}
			if( $card->status == 1 ){
				return $this->ReError('当前会员卡已被绑定！');	
			}
			if( $card->password != $password ){
				return $this->ReError('账密错误！');	
			}
			
			if(!$phone){
				return $this->ReSucess('通过验证！');	
			}else{
				
				$msgapi = new MsgApi;
				if (!$msgapi->verifyCode($phone,$code) ){
					return $this->ReError( $msgapi->errorInfo ); 
				}
				
				$res = model("user_vip")->save([
					"uid" =>$uid,
					"phone" => $phone,
					"level" => $card ->level,
					"password" => md5($card ->password),
					"code"=> $card ->code,
					"name"=>$name,
					"sex"=> $sex,
					"birthday"=> $birthday
				]);
				
				$res = $res &&  model("user")->where('id',$uid)
					->inc('integral',$card ->integral)
					->inc('balance',$card ->balance)
					->inc('balance_gift',$card ->balance_gift)
	    			->update();
	    			
	    		$card->bind_uid = $uid;
				$card->bind_time = date('Y-m-d H:i:s');
				$card->status = 1;
				$res = $res && $card->save();	
				
				if( !$res  ){
					return $this->ReError('异常错误');	
				}
				
				return $this->ReSucess($res);	
				
			}
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 会员注册
	* /app/v1/user_vip/register
	* 是否需要登录 
	* @is_need_login true
	*/
	public function register(){
		$param = request() ->post();
		$param['uid'] = request() ->userId;
		$param['password'] = md5($param['password']);
		try {
			$db = model("UserVip");
			if( !$res = $db->add($param) ){
				return $this->ReError($db->getError());	
			}
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 修改资料
	* /app/v1/user_vip/edit
	* 是否需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function edit($phone='',$name='',$birthday=''){
		if(!$name || !$birthday){
			return $this->ReError('必填参数错误');	
		}
		try {
			$vip = request()->userInfo->vip_info;
			if($phone) $vip->phone = $phone;
			$vip->name = $name;
			$vip->birthday = $birthday;
			$vip->save();
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 更新密码
	* /app/v1/user_vip/updatePwd
	* 是否需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function updatePwd($pwd,$pwd_new){
		if(!$pwd || !$pwd_new){
			return $this->ReError('必填参数错误');	
		}
		$uid = request() ->userId;
		$pwd = md5($pwd);
		$pwd_new = md5($pwd_new);
		try {
			$vip = model("UserVip")->where('uid',$uid)->where('password',$pwd)->find();
			if( !$vip ){
				return $this->ReError('原密码错误！');	
			}
			$vip->password = $pwd_new;
			$vip->save();
			return $this->ReSucess($res);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 会员充值-获取充值规则列表
	* /app/v1/user_vip/getRules
	* 是否需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function getRechargeRules(){
		try {
			return $this->ReSucess( model("userRechargeRule")->getValidList() );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 会员充值-获取支付签名
	* /app/v1/user_vip/recharge
	* 是否需要登录 
	* @is_need_login true
	* 是否需要注册会员信息
	* @is_vip true
	*/
	public function recharge($price,$mode="h5"){
		if( !$price ){
			return $this->ReError('参数错误！');	
		}
		
		$uid = request() ->userId;
		
		try {
			$wxapi = new WxApi($mode);
			$db = model("UserRecharge");
			$order = $db->where('status',0)
				->where('uid',$uid)
				->where('price',$price)
				->limit(1)
				->order("add_time desc")
				->find();
			if($order && !$wxapi->orderQuery($order->no)){
				$order->add_time=date("Y-m-d H:i:s");
				$order->save();
			}else{
				$order = $db->add( ['price'=>$price,'uid'=>$uid ] );
			}
			
			$paySign = $wxapi->createOrder($order->no,$order->price);
			
			return $this->ReSucess(compact('order', 'paySign'));	
			
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 会员充值-获取充值状态
	* /app/v1/user_vip/rechargeStatus
	* 是否需要登录 
	* @is_need_login true
	* @is_vip true
	*/
	public function rechargeStatus(){
		try {
			$status = model("UserRecharge")->getRechargeStatus(request() ->userId);
			return $this->ReSucess($status);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
}



